<!--
    template:模版区视图区
        - table控件
    script:逻辑区(控制视图区的显示)
-->
<template>
  <div>
    <br>
    <el-form :inline="true">
      <el-form-item label="活动名称">
        <el-input v-model="searchMap.name" placeholder="活动名称"></el-input>
      </el-form-item>
      <el-form-item label="开始日期">
        <el-date-picker v-model="searchMap.starttime_1" type="date" placeholder="选择开始日期"></el-date-picker>
        <el-date-picker v-model="searchMap.starttime_2" type="date" placeholder="选择截至日期"></el-date-picker>
      </el-form-item>
      <el-button type="primary" @click="feechData">查询</el-button>
      <el-button type="primary" @click="handleEdit('')">新增</el-button>
    </el-form>
    <el-table :data="list" border style="width: 100%">
      <el-table-column prop="id" label="活动ID" width="180"></el-table-column>
      <el-table-column prop="name" label="活动名称" width="180"></el-table-column>
      <el-table-column prop="sponsor" label="主办方" width="180"></el-table-column>
      <el-table-column prop="starttime" label="开始时间" width="180"></el-table-column>
      <el-table-column prop="endtime" label="结束时间" width="180"></el-table-column>
      <el-table-column prop="state" label="是否可见" width="180"></el-table-column>
      <el-table-column prop="city" label="城市" width="180"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleEdit(scope.row.id)" type="text" size="small">修改</el-button>
          <el-button @click="deleteById(scope.row.id)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="feechData"
      @current-change="feechData"
      :current-page="cuurentPage"
      :page-sizes="[5, 10, 20]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <el-dialog title="编辑" :visible.sync="dislogFormVisble">
      <el-form label-width="80px">
        <el-form-item label="活动名称">
          <el-input v-model="pojo.name" placeholder="活动名称"></el-input>
        </el-form-item>
        <el-form-item label="基本地址">
          <el-input v-model="pojo.address" placeholder="基本地址"></el-input>
        </el-form-item>
        <el-form-item label="开始日期">
          <el-date-picker type="date" v-model="pojo.starttime" placeholder="开始日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="截至日期">
          <el-date-picker type="date" v-model="pojo.endtime" placeholder="截至日期"></el-date-picker>
        </el-form-item>
        <el-form-item label="报名截至">
          <el-date-picker type="date" v-model="pojo.enrolltime" placeholder="报名截至"></el-date-picker>
        </el-form-item>
        <el-form-item label="活动详情">
          <el-input v-model="pojo.detail" placeholder="活动详情" type="textarea" :rows="2"></el-input>
        </el-form-item>
        <el-form-item label="城市">
          <el-select v-model="pojo.city" placeholder="选择城市">
            <el-option v-for="item in cityList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否可见">
          <el-switch
            v-model="pojo.status"
            active-value="1"
            inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
          ></el-switch>
          <el-form-item>
            <el-button type="primary" @click="saveData()">保存</el-button>
            <el-button @click="dislogFormVisble=false" type="danger">关闭</el-button>
          </el-form-item>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import gatheringApi from "@/api/gathering";
import cityApi from "@/api/city";
export default {
  //定义成员变量，定义页面属性
  data() {
    return {
      //接收列表
      total: 0, //总记录数
      list: [],
      cuurentPage: 1, //当前页
      pageSize: 10, //每页大小
      searchMap: {}, //查询条件
      dislogFormVisble: false, //编辑窗口是否可见
      pojo: {},
      cityList: [], //城市列表
      id: "" //当前用户修改的id
    };
  },
  //钩子函数
  created() {
    this.feechData();
    cityApi.getList().then(response => {
      this.cityList = response.data;
    });
  },
  //所有的方法
  methods: {
    feechData() {
      gatheringApi
        .getListByPage(this.cuurentPage, this.pageSize, this.searchMap)
        .then(response => {
          this.list = response.data.rows;
          this.total = response.data.total;
        });
    },
    saveData() {
      gatheringApi.update(this.id, this.pojo).then(response => {
        this.$message({
          message: response.message,
          type: response.flag ? "success" : "error"
        });
        if (response.flag) {
          this.feechData();
        }
      });

      this.dislogFormVisble = false; //关闭窗口
    },
    //根据ID查询活动信息
    handleEdit(id) {
      //赋值给id
      this.id = id;
      //打开编辑窗口
      this.dislogFormVisble = true;
      if (id != "") {
        //修改数据
        gatheringApi.findById(id).then(response => {
          if ((response.flag = true)) {
            //定义实体，用于数据回显
            this.pojo = response.data;
          }
        });
      } else {
        this.pojo = {};
      }
    },
    //根据ID删除活动信息
    deleteById(id) {
      this.$confirm("确定要删除此记录吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          gatheringApi.deleteById(id).then(response => {
            this.$message({
              message: response.message,
              type: response.flag ? "success" : "error"
            });
            if (response.flag) {
              this.feechData(); //刷新数据
            }
          });
        })
        .catch(() => {});
    }
  }
};
</script>
